home *** CD-ROM | disk | FTP | other *** search
RISC OS BBC BASIC V Source | 1995-06-15 | 10.1 KB | 294 lines |
- Module CalLib
- -----------------------------------------------------------------------
- Calendar Routines (version 1.01 12-Jun-95)
- Based on Version 1.20 - (C) Ian Palmer, 1st March 1993, supplied with
- BLibII. various routines added and renamed by Paul Hobbs 28-Oct-94
- This library contains several routines related to the calendar.
- In all cases months are numbered from 1 to 12 (1 = January, etc.)
- and years are the full A.D. year, (eg. 1993, etc.).
- -----------------------------------------------------------------------
- Public Methods Supported:
- FN_shell_CalLibInit
- FNshell_CalendarDayOfWeek
- FNshell_CalendarWeekDayString
- FNshell_CalendarMonthString
- FNshell_CalendarMonthNumber
- FNshell_CalendarDateString
- FNshell_CalendarDateValid
- FNshell_CalendarDaysInMonth
- FNshell_CalendarDaysInYear
- FNshell_CalendarLeapYear
- FNshell_CalendarDayOfYear
- FNshell_CalendarDaysBetween
- PROCshell_CalendarSetMonthString
- PROCshell_CalendarSetDayString
- PROCshell_CalendarGetTodaysDate
- Private Methods Supported:
- None
- *|Start FN_shell_CalLibInit
- _shell_CalLibInit
- Initialise Calendar module (constants/variables etc). Returns TRUE if module
- was initialised correctly..
- Define Module Variables
- -AM_Calendar_MonthStr$ = "JanFebMarAprMayJunJulAugSepOctNovDec"
- .2M_Calendar_DayStr$ = "SunMonTueWedThuFriSat"
- Define Module Constants
- 1!M_Calendar_MonthStrErr% = 99
- 28M_Calendar_MonthStrLen% =
- ( M_Calendar_MonthStr$ )
- M_Calendar_MonthStrErr$ = "String passed to CalendarSetMonthString must be " +
- ( M_Calendar_MonthStrLen% ) + " characters long"
- 5$M_Calendar_MonthInvalid$ = "***"
- 6$M_Calendar_DayInvalid$ = "***"
- 7!M_Calendar_DateInvalid$ = ""
- 83M_Calendar_DayStrLen% =
- ( M_Calendar_DayStr$ )
- 9~M_Calendar_DayStrErr$ = "String passed to CalendarSetDayString must be " +
- ( M_Calendar_DayStrLen% ) + " characters long"
- *|Stop FN_shell_CalLibInit
- A,*|Start PROCshell_CalendarSetMonthString
- shell_CalendarSetMonthString( month_name_string$ )
- Routine to set the string containing the 3 character month names used by the other routines
- so that foreign language versions of the program can be easily produced (normally string would
- be included in the message file of the application).
- Error if length of specified string is different to the default string..
- ( month_name_string$ ) <> M_Calendar_MonthStrLen%
- M_Calendar_MonthStrErr%, M_Calendar_MonthStrErr$
- M-M_Calendar_MonthStr$ = month_name_string$
- Q+*|Stop PROCshell_CalendarSetMonthString
- U**|Start PROCshell_CalendarSetDayString
- shell_CalendarSetDayString( day_name_string$ )
- Routine to set the string containing the 3 character day names used by the other routines
- so that foreign language versions of the program can be easily produced (normally string would
- be included in the message file of the application).
- Error if length of specified string is different to the default string..
- ( day_name_string$ ) <> M_Calendar_DayStrLen%
- M_Calendar_MonthStrErr%, M_Calendar_DayStrErr$
- a)M_Calendar_DayStr$ = day_name_string$
- e)*|Stop PROCshell_CalendarSetDayString
- i%*|start FNshell_CalendarDayOfWeek
- shell_CalendarDayOfWeek( day%, month%, year% )
- Returns the day of the week for a given date : 1 = Sun, 2 = Mon, etc.
- Q%, N%, X%
- N% = month%
- X% = year%
- month% <= 2
- N% = month% + 12 : X% = year% - 1
- rTQ% = ( day% + 2.6 * ( N% + 1) + X% + ( X%
- 4 ) - (X%
- 100) + (X%
- 400 ) )
- year% < 1752
- ( year% = 1752
- ( month% < 9
- ( month% = 9
- day% < 3 ) ) )
- Q% = Q% - 3
- Q% < 1
- Q% = Q% + 7
- shell_CalendarDateValid( day%, month%, year% ) )
- Q% = 0
- ~$*|stop FNshell_CalendarDayOfWeek
- )*|start FNshell_CalendarWeekDayString
- shell_CalendarWeekDayString( day_nr% )
- Returns a three letter version of a day of the week (1 -> "Sun",
- 2 -> "Mon", etc.)
- day_nr% < 1
- day_nr% > 7
- = M_Calendar_DayInvalid$
- M_Calendar_DayStr$, ( day_nr% - 1) * 3 + 1, 3 )
- (*|stop FNshell_CalendarWeekDayString
- '*|start FNshell_CalendarMonthString
- shell_CalendarMonthString( month_nr% )
- Returns a three letter version of a month (1 -> "Jan", 2 -> "Feb", etc.)
- month_nr% < 1
- month_nr% > 12
- = M_Calendar_MonthInvalid$
- M_Calendar_MonthStr$, ( month_nr% - 1 ) * 3 + 1, 3 )
- &*|stop FNshell_CalendarMonthString
- '*|start FNshell_CalendarMonthNumber
- shell_CalendarMonthNumber( month$ )
- Returns a month number given a 3 letter string ("Jan" -> 1, "Feb" -> 2, etc.)
- result%
- ( month$ ) <> 3
- M_Calendar_MonthStr$, month$ )
- < result% = (
- M_Calendar_MonthStr$, month$ ) + 2 ) / 3
- result% = 0
- = result%
- &*|stop FNshell_CalendarMonthNumber
- &*|start FNshell_CalendarDateString
- shell_CalendarDateString( day_nr%, month_nr%, year% )
- Given a date, returns a string version eg.
- FNshell_CalendarDateString( 18, 10, 1991 ) :
- Fri 18th Oct 1991
- day_of_week%, date_string$, N%
- date_string$ = ""
- shell_CalendarDateValid( day_nr%, month_nr%, year% )
- K day_of_week% =
- shell_CalendarDayOfWeek( day_nr%, month_nr%, year% )
- H date_string$ =
- shell_CalendarWeekDayString( day_of_week% ) + " "
- day_nr% < 10
- date_string$ += " "
- " date_string$ +=
- ( day_nr% )
- N% = day_nr%
- N% = 1
- day_nr% <> 11 date_string$ += "st" :
- N% = 2
- day_nr% <> 12 date_string$ += "nd" :
- N% = 3
- day_nr% <> 13 date_string$ += "rd" :
- date_string$ += "th"
- V date_string$ += " " +
- shell_CalendarMonthString( month_nr% ) + " " +
- ( year% )
- , date_string$ = M_Calendar_DateInvalid$
- = date_string$
- %*|stop FNshell_CalendarDateString
- %*|start FNshell_CalendarDateValid
- shell_CalendarDateValid( day_nr%, month_nr%, year% )
- Given a date returns a boolean value TRUE date is valid,
- FALSE date is not valid.
- day_nr% < 1
- month_nr% < 1
- year% < 1
- day_nr% > 30
- ( month_nr% = 4
- month_nr% = 6
- month_nr% = 9
- month_nr% = 11 )
- day_nr% > 31
- month_nr% > 12
- month_nr% = 2
- day_nr% > 29
- month_nr% = 2
- day_nr% = 29
- ( year%
- 4 ) <> 0
- month_nr% = 2
- day_nr% = 29
- ( year%
- 100 ) = 0
- ( year%
- 400 ) > 0
- handle the missing days in September 1752 when calendar was changed over..
- year% = 1752
- month_nr% = 9
- day_nr% > 2
- day_nr% < 14
- $*|stop FNshell_CalendarDateValid
- '*|Start FNshell_CalendarDaysInMonth
- shell_CalendarDaysInMonth( month_nr%, year% )
- Given a month number, returns number of days in the given month.
- nr_of_days%
- month_nr% > 0
- year% > 0
- month_nr% >= 1
- month_nr% <= 12
- month_nr%
-
- 1,4,6,9,11:
- nr_of_days% = 30
-
- 0
- shell_CalendarLeapYear( year% )
- nr_of_days% = 29
-
- nr_of_days% = 28
-
-
- nr_of_days% = 31
-
- nr_of_days% = 0
- = nr_of_days%
- &*|Stop FNshell_CalendarDaysInMonth
- &*|start FNshell_CalendarDaysInYear
- shell_CalendarDaysInYear( year% )
- Given a year, returns number of days in the given year.
- result%
- year% > 0
- year% = 1752
- result% = 355
- 8 result% = 365 -
- shell_CalendarLeapYear( year% )
- result% = 0
- = result%
- %*|stop FNshell_CalendarDaysInYear
- $*|start FNshell_CalendarLeapYear
- shell_CalendarLeapYear( year% )
- TRUE if given year is a leap year, FALSE if not.
- result%
- year% > 1
- 'U result% = ( ( year%
- 4 ) = 0
- ( ( year%
- 100 ) > 0
- ( year%
- 400 ) = 0 ) )
- result% = 0
- = result%
- -#*|stop FNshell_CalendarLeapYear
- 1%*|start FNshell_CalendarDayOfYear
- shell_CalendarDayOfYear( D%, M%, Y% )
- Given a date, returns the what day of that year that date is
- (ie. 1 to 366), or 0 if date is invalid
- day_of_year%
- shell_CalendarDateValid( D%, M%, Y% )
- day_of_year% = D%-31*(M%>1)-28*(M%>2)-31*(M%>3)-30*(M%>4)-31*(M%>5)-30*(M%>6)-31*(M%>7)-31*(M%>8)-30*(M%>9)-31*(M%>10)-30*(M%>11)-((M%>2)
- shell_CalendarLeapYear(Y%))+11*(Y%=1752
- ((M%=9
- D%>13)
- M%>9))
- day_of_year% = 0
- = day_of_year%
- A$*|stop FNshell_CalendarDayOfYear
- E'*|start FNshell_CalendarDaysBetween
- shell_CalendarDaysBetween( D%, M%, Y%, d%, m%, y% )
- Given two dates, this function returns the number of days between
- them. If the first date is after the second, then the result will
- be negative. If either date is invalid then returns 0.
- A%, B%, T%, N%, nr_days%
- T% = 0
- N% = 1
- shell_CalendarDateValid( D%, M%, Y% )
- shell_CalendarDateValid( d%, m%, y% )
- y% < Y%
- ( Y% = y%
- m% < M% )
- ( Y% = y%
- m% = M%
- d% < D% )
- N% = -1 :
- D%,d % :
- M%, m% :
- Y%, y%
- Y% < y%
- A% = Y%
- ( y% - 1 )
- U2 T% = T% +
- shell_CalendarDaysInYear( A% )
- V
- Xn nr_days% = N% * ( T% +
- shell_CalendarDayOfYear( d%, m%, y% ) -
- shell_CalendarDayOfYear( D%, M%, Y% ) )
- nr_days% = 0
- = nr_days%
- _&*|stop FNshell_CalendarDaysBetween
- c+*|start PROCshell_CalendarGetTodaysDate
- shell_CalendarGetTodaysDate(
- day%,
- month%,
- year% )
- Fills the three variables passed to it with today's date, taken from TIME$
- Will very likely fail if TIME$ is not in default English format..
- day% =
- $, 5 ) )
- year% =
- $, 12 ) )
- k@month% = (
- M_Calendar_MonthStr$,
- $, 8, 3 ) ) + 2 )
- o**|stop PROCshell_CalendarGetTodaysDate
-